package 简单;

import java.util.Stack;

public class 判断输入字符是否有效 {

    public static void main(String[] args) {
        String str = "(){[]}{()[}]";
        System.out.println(isValid(str));

    }

    public static boolean isValid(String str){
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c=='('||c=='{'||c=='['){
                stack.push(c);
            }else if (c==')'){
                if (stack.pop()!='('){
                    return false;
                }
            }else if (c==']'){
                if (stack.pop()!='['){
                    return false;
                }
            }else if (c=='}'){
                if (stack.pop()!='{'){
                    return false;
                }
            }
        }
        return stack.empty();
    }
}
